package com.fish.admin.module.system.manager;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fish.admin.module.system.domain.dto.SysUserPageQueryDTO;
import com.fish.admin.module.system.domain.entity.SysRole;
import com.fish.admin.module.system.domain.entity.SysUser;
import com.fish.admin.module.system.domain.vo.SysUserVO;
import com.fish.admin.module.system.mapper.SysUserMapper;
import com.fish.common.mybatisplus.util.PageUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class SysUserManager {

    private final SysUserMapper  sysUserMapper;


    public SysUser getById(Long userId) {
        return sysUserMapper.selectById(userId);
    }

    public Page<SysUserVO> page(SysUserPageQueryDTO pageQuery) {
        MPJLambdaWrapper<SysUser> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(SysUser.class)
                .selectAs(SysRole::getId, SysUserVO::getRoleId)
                .selectAs(SysRole::getName, SysUserVO::getRoleName)
                .leftJoin(SysRole.class, on->on.eq(SysUser::getRoleId, SysRole::getId));
        return sysUserMapper.selectJoinPage(PageUtils.build(pageQuery), SysUserVO.class, wrapper);
    }

    public void save(SysUser sysUser) {
        sysUserMapper.insert(sysUser);
    }

    public void deleteById(Long id) {
        sysUserMapper.deleteById(id);
    }

    public void updateById(SysUser sysUser) {
        sysUserMapper.updateById(sysUser);
    }
}
